/**
*机房 js
*/ 

var	machineroomDS;
var machineroomFormPanel;
function doMRd(id)
{
	Ext.MessageBox.confirm('确认删除', '确认删除该信息?', 
	function(btn) {
		if(btn == 'yes') // 选中了是按钮	
		{
			JMachineRoomWeb.deleteMachineRoomBO(id,function(data)
			{
				if(data)
				{ 
					Ext.Msg.alert('成功',"操作成功！"); 
					machineroomDS.load({params:{start:0,limit:17,'<%=Constant.BTS_CODE%>':'<%=strbtscode%>'}});	 
					//machineroomFormPanel.getForm().reset();			
				}else
				{
					Ext.Msg.alert("失败","操作失败!");
				}
			})
		}
	})	
}


Ext.onReady(function(){  
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'side';

	var machineroomType = Ext.data.Record.create([								
							   {name: 'id', type: "string"},
							   {name: 'btscode', type: "string"},
							   {name: 'btsname', type: "string"},
							   {name: 'frame', type: "string"},
							   {name: 'acreageT', type: "string"}, 
							   {name: 'plan', type: "string"}, 
							   {name: 'property', type: "string"}, 
							   {name: 'fromunit', type: "string"},
							   {name: 'other', type: 'string'},
							   {name: 'contact',  type: 'string'},
							   {name: 'telphone',  type: 'string'},
							   {name: 'opentime',  type: 'date'},
							   {name: 'wallheight',  type: 'string'},
							   {name: 'doortype',  type: 'string'},
							   {name: 'facility',  type: 'string'},
							   {name: 'reason',  type: 'string'},
							   {name: 'warnschilder',  type: 'string'},
							   {name: 'acreageU',  type: 'string'},
							   {name: 'place',  type: 'string'},
							   {name: 'maintenance',  type: 'string'},
							   {name: 'refreshtime',  type: 'date'},
							   {name: 'memo',  type: 'string'},
							   {name: 'mroomIsLook',  type: 'string'},
							   {name: 'mroomLookTel',  type: 'string'}
						 ]);
	var machineroomCM = new Ext.grid.ColumnModel(
			[
				new Ext.grid.CheckboxSelectionModel({singleSelect: true}), 	            
				{header: "流水号", sortable: true, dataIndex: 'id',align:'left'},
				{header: "基站编号", sortable: true, dataIndex: 'btscode',align:'left'},
				{header: "基站名称",  sortable: true, dataIndex: 'btsname',align:'left'},	
				{header: "机房结构",  sortable: true, dataIndex: 'frame',align:'left'},	
				{header: "机房面积（平方米）",  sortable: true, dataIndex: 'acreageT',align:'left'},	
				{header: "机房平面布置图",  sortable: true, dataIndex: 'plan',align:'left'},	      
				{header: "删除", dataIndex: 'id',renderer: showDelete}					
			]
		);				 
	
	function showDelete(id)
	{
		return "<input type='button' value='删除' class='x-btn button' onclick='doMRd(\"" + id  +   "\")'>";  
	}
			
	machineroomDS = new Ext.data.Store({
				proxy: new Ext.data.DWRProxy(JMachineRoomWeb.getMachineRoomBOList, true),	
				reader: new Ext.data.ListRangeReader({
					totalProperty: 'totalSize',
					id: 'id'
				}, machineroomType),
				sortInfo:{field:'id', direction:'ASC'}// 排序信息
			});

	
	var towerMountedGridDiv = Ext.get("mainTabPanelDIV");
	var machineroomGrid = new Ext.grid.GridPanel({			 	
			ds: machineroomDS,
			cm: machineroomCM, 
			sm: new Ext.grid.RowSelectionModel({
                    singleSelect: true,
                    listeners: {
                        rowselect: function(sm, row, rec) {
                            machineroomFormPanel.getForm().loadRecord(rec);
                        }
                    }
                }),
			autoWidth : true,  
			autoScroll : true,  
			autoHeight　: false,
			region : 'center',
			height: 400,
			minSize: 200,		
			frame:true,
			collapsible:false, 
			enableDragDrop:false, 		
			bbar: new Ext.PagingToolbar({
					pageSize: 17,
					store: machineroomDS,
					displayInfo: true,
					displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
					emptyMsg: "没有记录"
				})
		});

	machineroomDS.on('load', function() {
		machineroomGrid.getSelectionModel().selectFirstRow(); 
	});		

	var frameStore = new Ext.data.Store({  
		        proxy:new Ext.data.DWRProxy(JCommWeb.getDirctoryByType,{}),  
		        reader:new Ext.data.DWRArrayReader(
						{},
						new Ext.data.Record.create([  
							{name: 'text', mapping:1},       
							{name: 'value',mapping:0}                   
						  ])
						)  
		    }); 
	
	var frameCombo = new Ext.form.ComboBox({	   
			fieldLabel: '机房结构',
			name: 'frame',	
			width:100,
			valueField :"value",
			displayField: "text",
			mode: 'remote',
			selectOnFocus:true,                    
			forceSelection: true,  
			hiddenName:'frame',
			triggerAction: 'all', 
			editable:false, 
			store: frameStore
		});	
	
	frameStore.on('beforeload', function() {
			  frameStore.baseParams = {
					DIRECTORY_TYPE:'03'   
			  };
		});	
	frameStore.load({params:{DIRECTORY_TYPE:'03'}});
	

	
	var propertyStore  = new Ext.data.Store({  
			proxy:new Ext.data.DWRProxy(JCommWeb.getDirctoryByType,{}),  
			reader:new Ext.data.DWRArrayReader(
					{},
					new Ext.data.Record.create([  
						{name: 'text', mapping:1},       
						{name: 'value',mapping:0}                   
					  ])
					)  
		});
	var propertyCombo = new Ext.form.ComboBox({	   
		fieldLabel: '产权',
		name: 'property',	
		width:100,
		valueField :"value",
	    displayField: "text",
	    mode: 'local',
		selectOnFocus:true,                    
		forceSelection: true, 
		hiddenName:'property',
		triggerAction: 'all', 
		editable:false, 
		store: propertyStore
	});
	propertyStore.on('beforeload', function() {
			  propertyStore.baseParams = {
					DIRECTORY_TYPE:'04'   
			  };
		});	
	propertyStore.load({params:{DIRECTORY_TYPE:'04'}});
	
	var doortypeStore  = new Ext.data.Store({  
			proxy:new Ext.data.DWRProxy(JCommWeb.getDirctoryByType,{}),  
			reader:new Ext.data.DWRArrayReader(
					{},
					new Ext.data.Record.create([  
						{name: 'text', mapping:1},       
						{name: 'value',mapping:0}                   
					  ])
					)  
		});
	var doortypeCombo = new Ext.form.ComboBox({	   
		fieldLabel: '围墙门类型',
		name: 'doortype',	
		width:100,
		valueField :"value",
	    displayField: "text",
	    mode: 'local',
		selectOnFocus:true,                    
		forceSelection: true, 
		hiddenName:'doortype',
		triggerAction: 'all', 
		editable:false, 
		store: doortypeStore
	});
	doortypeStore.on('beforeload', function() {
			  doortypeStore.baseParams = {
					DIRECTORY_TYPE:'05'   
			  };
		});	
	doortypeStore.load({params:{DIRECTORY_TYPE:'05'}});
	
	var facilityStore  = new Ext.data.Store({  
			proxy:new Ext.data.DWRProxy(JCommWeb.getDirctoryByType,{}),  
			reader:new Ext.data.DWRArrayReader(
					{},
					new Ext.data.Record.create([  
						{name: 'text', mapping:1},       
						{name: 'value',mapping:0}                   
					  ])
					)  
		});
	var facilityCombo = new Ext.form.ComboBox({	   
		fieldLabel: '进入机房是否方便',
		width:100,
		name: 'facility',	
		valueField :"value",
	    displayField: "text",
	    mode: 'local',
		selectOnFocus:true,                    
		forceSelection: true, 
		hiddenName:'facility',
		triggerAction: 'all', 
		editable:false, 
		store: facilityStore
	});
	facilityStore.on('beforeload', function() {
			  facilityStore.baseParams = {
					DIRECTORY_TYPE:'06'   
			  };
		});	
	facilityStore.load({params:{DIRECTORY_TYPE:'06'}});
	
	var mroomIsLookStore  = new Ext.data.Store({  
			proxy:new Ext.data.DWRProxy(JCommWeb.getDirctoryByType,{}),  
			reader:new Ext.data.DWRArrayReader(
					{},
					new Ext.data.Record.create([  
						{name: 'text', mapping:1},       
						{name: 'value',mapping:0}                   
					  ])
					)  
		});
	var mroomIsLookCombo = new Ext.form.ComboBox({	   
		fieldLabel: '是否看护',
		width:100,
		name: 'mroomIsLook',	
		valueField :"value",
	    displayField: "text",
	    mode: 'local',
		selectOnFocus:true,                    
		forceSelection: true, 
		hiddenName:'mroomIsLook',
		triggerAction: 'all', 
		editable:false, 
		store: mroomIsLookStore
	});
	mroomIsLookStore.on('beforeload', function() {
			  mroomIsLookStore.baseParams = {
					DIRECTORY_TYPE:'06'   
			  };
		});	
	mroomIsLookStore.load({params:{DIRECTORY_TYPE:'06'}});
	
	var warnschilderStore  = new Ext.data.Store({  
			proxy:new Ext.data.DWRProxy(JCommWeb.getDirctoryByType,{}),  
			reader:new Ext.data.DWRArrayReader(
					{},
					new Ext.data.Record.create([  
						{name: 'text', mapping:1},       
						{name: 'value',mapping:0}                   
					  ])
					)  
		});
	var warnschilderCombo = new Ext.form.ComboBox({	   
		fieldLabel: '有无机房警示牌',
		name: 'warnschilder',	
		valueField :"value",
		width:100,
	    displayField: "text",
	    mode: 'local',
		selectOnFocus:true,                    
		forceSelection: true, 
		hiddenName:'warnschilder',
		triggerAction: 'all', 
		editable:false, 
		store: warnschilderStore
	});
	warnschilderStore.on('beforeload', function() {
			  warnschilderStore.baseParams = {
					DIRECTORY_TYPE:'07'   
			  };
		});	
	warnschilderStore.load({params:{DIRECTORY_TYPE:'07'}});
	
	
	
	machineroomFormPanel = new Ext.form.FormPanel({
		region     : 'south', 
		bodyStyle  : 'padding: 10px; background-color: #DFE8F6',
		labelWidth : 130,  
		height: 210,
		minSize: 230,
		maxSize: 230,
		frame:true,
		buttonAlign:'right',
		layout:'column', 
		items:[
			 {
				columnWidth:.25,
				layout: 'form',  
				labelAlign:'right',	
				labelWidth:120,	 
				items: [
					{
						xtype:'hidden',
						fieldLabel: '流水号', 
						name: 'id'
					}, {
						xtype:'textfield',
						readOnly: true,
						width:100,
						fieldLabel: '基站编号', 
						name: 'btscode'
					}, {
						xtype:'textfield',
						fieldLabel: '基站名称', 
						readOnly: true,
						width:100,
						name: 'btsname'
					}, frameCombo,
					{
						xtype:'numberfield',
						decimalPrecision:2,
						value:1,
						fieldLabel: '机房面积（平方米）', 
						width:100,						
						name: 'acreageT'
					},{
						xtype:'textfield',
						fieldLabel: '机房平面布置图', 
						width:100,
						name: 'plan'
					},propertyCombo
					]
			 },
			 {
				columnWidth:.25,
				layout: 'form',  
				labelAlign:'right',	
				labelWidth:110,	 
				items: [					
					{
						xtype:'textfield',
						fieldLabel: '租用或购买何单位',
						width:100,
						name: 'fromunit'
					}, {
						xtype:'textfield',
						fieldLabel: '其它',
						width:100,
						name: 'other'
					}
					, {
						xtype:'textfield',
						fieldLabel: '联系人',
						width:100,
						name: 'contact'
					}
					, {
						xtype:'textfield',
						fieldLabel: '联系电话',
						width:100,
						name: 'telphone'
					}, {
						xtype:'datefield',
						fieldLabel: '机房启用时间',	
						width:100,			
						allowBlank:false,						
						format : "Y-m-d",
						name: 'opentime'
					}, {
						xtype:'numberfield',
						decimalPrecision:2,
						value:1,
						fieldLabel: '围墙高度（米）', 
						width:100,
						name: 'wallheight'
					}]
			 },
			 {
				columnWidth:.25,
				layout: 'form',  
				labelWidth:110,	 
				labelAlign:'right',	
				items: [
					doortypeCombo,
					facilityCombo,
					{
						xtype:'textfield',
						fieldLabel: '原因说明', 
						width:100,
						name: 'reason'
					},
					mroomIsLookCombo
					,{
						xtype:'textfield',
						fieldLabel: '看护电话', 
						width:100,
						name: 'mroomLookTel'
					}]
			 },{
				columnWidth:.25,
				layout: 'form',  
				labelWidth:130,	 
				labelAlign:'right',	
				items: [
					warnschilderCombo,
					{
						xtype:'numberfield',
						decimalPrecision:2,
						value:1,
						fieldLabel: '使用面积（平方米）', 
						width:100,
						name: 'acreageU'
					},
					{
						xtype:'textfield',
						fieldLabel: '建筑位置', 
						width:100,
						name: 'place'
					},
					{
						xtype:'textfield',
						fieldLabel: '机房整治及维修记录', 
						width:100,
						name: 'maintenance'
					}
					,{
						xtype:'textarea',
						fieldLabel: '备注', 
						width:100,
						name: 'memo'
					}]
			 }
			],
		buttons :[
			{
				text : ' 保存',
				handler : savemachineroom
			},{
				text : ' 新建/取消',
				handler : function(){
					machineroomFormPanel.getForm().reset(); 
					machineroomFormPanel.getForm().loadRecord(recordForAll);	
				}
			}
			]	
	});



	//Simple 'border layout' panel to house both grids
	
	var displayPanel = new Ext.Panel({
		width    : towerMountedGridDiv.getComputedWidth(),
		height   : 500,
		layout   : 'border',
		renderTo : 'machineroomTab',
		items    : [
			machineroomGrid,
			machineroomFormPanel
		]
	});
	
	
	function savemachineroom()
	{
		if (!machineroomFormPanel.getForm().isValid())
			return;	
			
		var form = machineroomFormPanel.getForm().getValues(false);	
		MachineRoomBO = {};
		MachineRoomBO.id = form.id;
		MachineRoomBO.btscode = form.btscode;
		MachineRoomBO.btsname= form.btsname; 
		MachineRoomBO.frame= form.frame; 
		MachineRoomBO.acreageT= form.acreageT; 
		MachineRoomBO.plan= form.plan; 
		MachineRoomBO.property= form.property; 
		MachineRoomBO.fromunit= form.fromunit;
		MachineRoomBO.other= form.other; 
		MachineRoomBO.contact= form.contact; 
		MachineRoomBO.telphone= form.telphone; 
		MachineRoomBO.wallheight= form.wallheight; 
		MachineRoomBO.doortype= form.doortype; 
		MachineRoomBO.facility= form.facility; 
		MachineRoomBO.reason= form.reason; 
		MachineRoomBO.warnschilder= form.warnschilder; 
		MachineRoomBO.acreageU= form.acreageU; 
		MachineRoomBO.place= form.place; 
		MachineRoomBO.maintenance= form.maintenance; 
		MachineRoomBO.memo= form.memo; 
		MachineRoomBO.mroomIsLook= form.mroomIsLook; 
		MachineRoomBO.mroomLookTel= form.mroomLookTel;  
		
		var opentimeDate = new Date();	
		var arrPlanTime = form.opentime.split("-");		
				
		var yearOth = arrPlanTime[0];
		var monthOth = arrPlanTime[1];
		var dayOth = arrPlanTime[2];
		opentimeDate.setFullYear(yearOth,monthOth-1,dayOth);
		
		MachineRoomBO.opentime = opentimeDate;
	   			
		JMachineRoomWeb.saveMachineRoomBO(MachineRoomBO,function(data)
		{
			if(data)
			{ 
				Ext.Msg.alert('成功',"操作成功！"); 
				loadDate(machineroomDS,machineroomFormPanel);	 
			}else
			{
				Ext.Msg.alert("失败","操作失败!");
			}
		})
			
	}
		
	loadDate(machineroomDS,machineroomFormPanel);
	
})
